package com.yaodao;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * @Author: 信息部 HN ZHAO
 * @Package:
 * @Description:
 * @Date: 2020/10/14 16:12
 */
public class TestTX {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("49.234.125.145",6379);

        Transaction multi = jedis.multi();//开启事务

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","YaoDao");
        jsonObject.put("work","Coder");
        String result = jsonObject.toJSONString();

        try {
            multi.set("user",result); //相当于 set user "{"name":"YaoDao" "work":"Coder"}"
            multi.exec();//执行事务
        }
        catch (Exception e){
            multi.discard();//放弃事务
        }
        finally {
            System.out.println(jedis.get("user"));
            jedis.close();//关闭连接
        }


    }
}
